package news.controller;

import com.alibaba.fastjson.JSON;
import news.service.*;
import news.util.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * @author Administrator
 * 添加主题
 */
@WebServlet("/queryPageNews")
public class QueryNewsController extends HttpServlet {
    IMainPageNewsService iMainPageNewsService = new MainPageNewsServiceImpl();
    IQueryNewsListService iQueryNewsListService = new QueryNewsListServiceImpl();

    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        //1.接受参数
        //2.存储数据


        PageBean pageBean = new PageBean();
        String str = request.getParameter("pageIndex");

        Integer pageIndex = (str == null || str.equals("")) ? 1:Integer.parseInt(str);

        int count = iQueryNewsListService.calcTotalCount();

        pageBean.setCurrPageNo(pageIndex);
        pageBean.setTotalCount(count);

        PageBean pageList = iMainPageNewsService.getNewsPublishPageList(pageBean);

        PrintWriter out = response.getWriter();
        String resJson = JSON.toJSONString(pageList);
        out.write(resJson);
        out.flush();
        out.close();

    }

}